﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using XZL.Web.Da;
using XZL.Web.Entity;

namespace XZL.Web.Biz
{
    public class CourseBiz
    {
        /// <summary>
        /// 获取课程列表
        /// </summary>
        /// <param name="name"></param>
        /// <param name="type"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageCount"></param>
        /// <returns></returns>
        public static List<Course> GetCourseList(string name, int type, int isCharge, int isLive, int pageIndex, int pageSize, ref int? pageCount)
        {
            CourseDBDataContext db = new CourseDBDataContext();
            List<Proc_CourseInfo_SelectListResult> ls = db.Proc_CourseInfo_SelectList(name, type, isCharge, isLive, pageIndex, pageSize, ref pageCount).ToList();
            List<Course> courseList = new List<Course>();

            if (ls != null)
            {
                foreach (var obj in ls)
                {
                    Course cor = new Course();
                    cor.CourseID = Convert.ToInt32(obj.CourseID);
                    if (obj.CourseImg != null && obj.CourseImg != "")
                    {
                        string imgPath = System.Configuration.ConfigurationManager.AppSettings["ImagePath"];
                        cor.CourseImg = imgPath + obj.CourseImg;
                    }
                    if (obj.CourseInfoImg != null && obj.CourseInfoImg != "")
                    {
                        string imgPath = System.Configuration.ConfigurationManager.AppSettings["ImagePath"];
                        cor.CourseInfoImg = imgPath + obj.CourseInfoImg;
                    }
                    cor.CourseIntroduce = obj.CourseIntroduce;
                    cor.CourseExplain = obj.CourseExplain;
                    cor.CourseName = obj.CourseName;
                    cor.CourseTypeID = Convert.ToInt32(obj.CourseType);
                    cor.CourseTypeName = obj.CourseTypeName;
                    cor.CreateTime = Convert.ToDateTime(obj.CreateTime);
                    cor.CourseTeamID = Convert.ToInt32(obj.CourseTeamID);
                    cor.IsCharge = Convert.ToInt32(obj.IsCharge);
                    cor.JoinCount = Convert.ToInt32(obj.JoinCount);
                    cor.Price = Convert.ToDouble(obj.Price);
                    cor.RelatedCourse = obj.RelatedCourse;
                    cor.StartTime = Convert.ToDateTime(obj.StartTime);               
                    cor.TeacherID = Convert.ToInt32(obj.TeacherID);
                    cor.TeacherID = Convert.ToInt32(obj.TeacherID);
                    cor.TeacherName = obj.TeacherName;
                    cor.TeacherTitle1 = obj.TeacherTitle1;
                    cor.TeacherTitle2 = obj.TeacherTitle2;
                    if (obj.TeacherImg != null && obj.TeacherImg != "")
                    {
                        string imgPath = System.Configuration.ConfigurationManager.AppSettings["ImagePath"];
                        cor.TeacherImg = imgPath + obj.TeacherImg;
                    }
                    cor.TeacherInfo = obj.TeacherInfo;


                    courseList.Add(cor);
                }
                return courseList;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获取课程信息
        /// </summary>
        /// <param name="courseID"></param>
        /// <returns></returns>
        public static Course GetCourseInfo(int courseID)
        {
            CourseDBDataContext db = new CourseDBDataContext();
            var obj = db.Proc_CourseInfo_SelectInfo(courseID).FirstOrDefault();
            if (obj != null)
            {
                Course cor = new Course();
                cor.CourseID = obj.CourseID;
                if (obj.CourseImg != null && obj.CourseImg != "")
                {
                    string imgPath = System.Configuration.ConfigurationManager.AppSettings["ImagePath"];
                    cor.CourseImg = imgPath + obj.CourseImg;
                }
                if (obj.CourseInfoImg != null && obj.CourseInfoImg != "")
                {
                    string imgPath = System.Configuration.ConfigurationManager.AppSettings["ImagePath"];
                    cor.CourseInfoImg = imgPath + obj.CourseInfoImg;
                }
                cor.CourseIntroduce = obj.CourseIntroduce;
                cor.CourseIntroduce = HttpUtility.HtmlDecode(obj.CourseIntroduce);
                cor.CourseExplain = obj.CourseExplain;
                cor.CourseName = obj.CourseName;
                cor.CourseTypeID = obj.CourseType;
                cor.CourseTypeName = obj.CourseTypeName;
                cor.CreateTime = Convert.ToDateTime(obj.CreateTime);
                cor.CourseTeamID = Convert.ToInt32(obj.CourseTeamID);
                cor.IsCharge = obj.IsCharge;
                cor.JoinCount = obj.JoinCount;
                cor.Price = obj.Price;
                cor.RelatedCourse = obj.RelatedCourse;
                cor.StartTime = Convert.ToDateTime(obj.StartTime);
                cor.VideoLink = obj.VideoLink;
                cor.TeacherID = obj.TeacherID;
                cor.TeacherName = obj.TeacherName;
                if (obj.TeacherImg != null && obj.TeacherImg != "")
                {
                    string imgPath = System.Configuration.ConfigurationManager.AppSettings["ImagePath"];
                    cor.TeacherImg = imgPath + obj.TeacherImg;
                }
                cor.TeacherInfo = obj.TeacherInfo;

                return cor;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获取视频列表
        /// </summary>
        /// <param name="courseID"></param>
        /// <returns></returns>
        public static List<Proc_CourseItem_SelectListByCourseIDResult> GetCourseItemList(int courseID, int index, int size, ref int? count)
        {
            string imgPath = System.Configuration.ConfigurationManager.AppSettings["ImagePath"];
            CourseDBDataContext db = new CourseDBDataContext();
            List<Proc_CourseItem_SelectListByCourseIDResult> ls = db.Proc_CourseItem_SelectListByCourseID(courseID, index, size, ref count).ToList();
            foreach (var obj in ls)
            {
                if (obj != null)
                {
                    if (obj.Image != null && obj.Image != "")
                    {
                        obj.Image = imgPath + obj.Image;
                    }
                }
            }
            return ls;
        }

        /// <summary>
        /// 获取视频信息
        /// </summary>
        /// <param name="itemID"></param>
        /// <returns></returns>
        public static Proc_CourseItem_SelectInfoByItemIDResult GetCourseItemInfo(int itemID)
        {
            string imgPath = System.Configuration.ConfigurationManager.AppSettings["ImagePath"];
            CourseDBDataContext db = new CourseDBDataContext();
            Proc_CourseItem_SelectInfoByItemIDResult item = db.Proc_CourseItem_SelectInfoByItemID(itemID).FirstOrDefault();
            if (item != null)
            {
                if (item.Image != "" && item.Image != null)
                {
                    item.Image = imgPath + item.Image;
                }
            }
            return item;
        }

        /// <summary>
        /// 获取首页banner列表
        /// </summary>
        /// <returns></returns>
        public static List<Proc_BannerInfo_SelectListResult> GetBannerList()
        {
            CourseDBDataContext db = new CourseDBDataContext();
            int? count = 0;
            List<Proc_BannerInfo_SelectListResult> ls = db.Proc_BannerInfo_SelectList("", 1, 10, ref count).ToList();
            foreach (var obj in ls)
            {
                if (obj.BannerImg != null && obj.BannerImg != "")
                {
                    string imgPath = System.Configuration.ConfigurationManager.AppSettings["ImagePath"];
                    obj.BannerImg = imgPath + obj.BannerImg;
                }
            }
            return ls;
        }

        /// <summary>
        /// 课程点击
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static bool CourseClick(int id)
        {
            CourseDBDataContext db = new CourseDBDataContext();
            db.Proc_CourseInfo_Click(id);
            return true;
        }

        /// <summary>
        /// 获取最热课程
        /// </summary>
        /// <returns></returns>
        public static List<Course> GetHotCourseList()
        {
            int? count = 0;
            CourseDBDataContext db = new CourseDBDataContext();
            List<Proc_CourseInfo_HottestListResult> ls = db.Proc_CourseInfo_HottestList(1, 100, ref count).ToList();
            List<Course> courseList = new List<Course>();

            if (ls != null)
            {
                foreach (var obj in ls)
                {
                    Course cor = new Course();
                    cor.CourseID = Convert.ToInt32(obj.CourseID);
                    if (obj.CourseImg != null && obj.CourseImg != "")
                    {
                        string imgPath = System.Configuration.ConfigurationManager.AppSettings["ImagePath"];
                        cor.CourseImg = imgPath + obj.CourseImg;
                    }
                    if (obj.CourseInfoImg != null && obj.CourseInfoImg != "")
                    {
                        string imgPath = System.Configuration.ConfigurationManager.AppSettings["ImagePath"];
                        cor.CourseInfoImg = imgPath + obj.CourseInfoImg;
                    }
                    cor.CourseIntroduce = obj.CourseIntroduce;
                    cor.CourseExplain = obj.CourseExplain;
                    cor.CourseName = obj.CourseName;
                    cor.CourseTypeID = Convert.ToInt32(obj.CourseType);
                    cor.CourseTypeName = obj.CourseTypeName;
                    cor.CreateTime = Convert.ToDateTime(obj.CreateTime);
                    cor.CourseTeamID = Convert.ToInt32(obj.CourseTeamID);
                    cor.IsCharge = Convert.ToInt32(obj.IsCharge);
                    cor.JoinCount = Convert.ToInt32(obj.JoinCount);
                    cor.Price = Convert.ToDouble(obj.Price);
                    cor.RelatedCourse = obj.RelatedCourse;
                    cor.StartTime = Convert.ToDateTime(obj.StartTime);
                    cor.TeacherID = Convert.ToInt32(obj.TeacherID);
                    cor.TeacherID = Convert.ToInt32(obj.TeacherID);
                    cor.TeacherName = obj.TeacherName;
                    cor.TeacherTitle1 = obj.TeacherTitle1;
                    cor.TeacherTitle2 = obj.TeacherTitle2;
                    if (obj.TeacherImg != null && obj.TeacherImg != "")
                    {
                        string imgPath = System.Configuration.ConfigurationManager.AppSettings["ImagePath"];
                        cor.TeacherImg = imgPath + obj.TeacherImg;
                    }
                    cor.TeacherInfo = obj.TeacherInfo;


                    courseList.Add(cor);
                }
                return courseList;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获取最新课程
        /// </summary>
        /// <returns></returns>
        public static List<Course> GetNewCourseList()
        {
            int? count = 0;
            CourseDBDataContext db = new CourseDBDataContext();
            List<Proc_CourseInfo_NewestListResult> ls = db.Proc_CourseInfo_NewestList(1, 100, ref count).ToList();
            List<Course> courseList = new List<Course>();

            if (ls != null)
            {
                foreach (var obj in ls)
                {
                    Course cor = new Course();
                    cor.CourseID = Convert.ToInt32(obj.CourseID);
                    if (obj.CourseImg != null && obj.CourseImg != "")
                    {
                        string imgPath = System.Configuration.ConfigurationManager.AppSettings["ImagePath"];
                        cor.CourseImg = imgPath + obj.CourseImg;
                    }
                    if (obj.CourseInfoImg != null && obj.CourseInfoImg != "")
                    {
                        string imgPath = System.Configuration.ConfigurationManager.AppSettings["ImagePath"];
                        cor.CourseInfoImg = imgPath + obj.CourseInfoImg;
                    }
                    cor.CourseIntroduce = obj.CourseIntroduce;
                    cor.CourseExplain = obj.CourseExplain;
                    cor.CourseName = obj.CourseName;
                    cor.CourseTypeID = Convert.ToInt32(obj.CourseType);
                    cor.CourseTypeName = obj.CourseTypeName;
                    cor.CreateTime = Convert.ToDateTime(obj.CreateTime);
                    cor.CourseTeamID = Convert.ToInt32(obj.CourseTeamID);
                    cor.IsCharge = Convert.ToInt32(obj.IsCharge);
                    cor.JoinCount = Convert.ToInt32(obj.JoinCount);
                    cor.Price = Convert.ToDouble(obj.Price);
                    cor.RelatedCourse = obj.RelatedCourse;
                    cor.StartTime = Convert.ToDateTime(obj.StartTime);
                    cor.TeacherID = Convert.ToInt32(obj.TeacherID);
                    cor.TeacherID = Convert.ToInt32(obj.TeacherID);
                    cor.TeacherName = obj.TeacherName;
                    cor.TeacherTitle1 = obj.TeacherTitle1;
                    cor.TeacherTitle2 = obj.TeacherTitle2;
                    if (obj.TeacherImg != null && obj.TeacherImg != "")
                    {
                        string imgPath = System.Configuration.ConfigurationManager.AppSettings["ImagePath"];
                        cor.TeacherImg = imgPath + obj.TeacherImg;
                    }
                    cor.TeacherInfo = obj.TeacherInfo;


                    courseList.Add(cor);
                }
                return courseList;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 添加我的课程
        /// </summary>
        /// <param name="courseID"></param>
        /// <param name="payType">1:自己购买   2:赠送   3:收藏</param>
        /// <param name="userID"></param>
        /// <param name="payUserID"></param>
        /// <param name="memo"></param>
        /// <returns></returns>
        public static int AddUserCourse(int courseID, int payType, string userID, string payUserID, string orderID,int count, string memo)
        {
            CourseDBDataContext db = new CourseDBDataContext();
            return db.Proc_UserCourse_Add(courseID, payType, userID, payUserID, orderID, count,memo);
        }

        /// <summary>
        /// 写账单
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="amount"></param>
        /// <param name="accountType"></param>
        /// <param name="payType"></param>
        /// <param name="orderID"></param>
        /// <param name="memo"></param>
        /// <returns></returns>
        public static bool AddAccountLog(string userID, double amount, int accountType, int payType, string orderID, string memo)
        {
            CourseDBDataContext db = new CourseDBDataContext();
            db.Proc_AccountLog_Add(userID, amount, accountType, payType, orderID, memo);
            return true;
        }

        /// <summary>
        /// 收藏课程
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="courseID"></param>
        /// <returns>1：收藏成功      2：取消收藏</returns>
        public static int CollectCourse(string userID, int courseID)
        {
            CourseDBDataContext db = new CourseDBDataContext();
            int res = db.Proc_UserCourse_Collect(userID, courseID);
            return res;
        }

        /// <summary>
        /// 判断是否 我的课程
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="courseID"></param>
        /// <param name="type">1:自己购买    2:别人赠送    3:收藏</param>
        /// <returns>1:是   -1:否</returns>
        public static int IsMyCourse(string userID, int courseID, int type)
        {
            CourseDBDataContext db = new CourseDBDataContext();
            return db.Proc_UserCourse_IsMyCourse(userID, courseID, type);
        }

        /// <summary>
        /// 获取我的课程信息
        /// </summary>
        /// <param name="ucID"></param>
        /// <returns></returns>
        public static Proc_UserCourse_SelectInfoResult GetUserCourseInfo(int ucID)
        {
            CourseDBDataContext db = new CourseDBDataContext();
            return db.Proc_UserCourse_SelectInfo(ucID).FirstOrDefault();
        }

        /// <summary>
        /// 获取我的课程
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="type"></param>
        /// <returns>1.我购买的  2.我的赠送  3.我的收藏   4.我收到的</returns>
        public static List<Proc_UserCourse_MyCourseListResult> GetMyCourseList(string userID, int type)
        {
            CourseDBDataContext db = new CourseDBDataContext();
            List< Proc_UserCourse_MyCourseListResult> ls = db.Proc_UserCourse_MyCourseList(userID, type).ToList();
            foreach (var obj in ls)
            {
                if (obj.CourseImg != null && obj.CourseImg != "")
                {
                    string imgPath = System.Configuration.ConfigurationManager.AppSettings["ImagePath"];
                    obj.CourseImg = imgPath + obj.CourseImg;
                }
            }
            return ls;
        }

        /// <summary>
        /// 获取推荐列表
        /// </summary>
        /// <param name="inviteStr"></param>
        /// <returns></returns>
        public static List<Course> GetInviteList(string inviteStr)
        {
            CourseDBDataContext db = new CourseDBDataContext();
            List<Course> courseList = new List<Course>();
            if (inviteStr == null || inviteStr == "")
            {
                return courseList;
            }
            List<Proc_CourseInfo_SelectInviteListResult> ls = db.Proc_CourseInfo_SelectInviteList(inviteStr).ToList();
            foreach (var obj in ls)
            {
                Course cor = new Course();
                cor.CourseID = Convert.ToInt32(obj.CourseID);
                if (obj.CourseImg != null && obj.CourseImg != "")
                {
                    string imgPath = System.Configuration.ConfigurationManager.AppSettings["ImagePath"];
                    cor.CourseImg = imgPath + obj.CourseImg;
                }
                if (obj.CourseInfoImg != null && obj.CourseInfoImg != "")
                {
                    string imgPath = System.Configuration.ConfigurationManager.AppSettings["ImagePath"];
                    cor.CourseInfoImg = imgPath + obj.CourseInfoImg;
                }
                cor.CourseIntroduce = obj.CourseIntroduce;
                cor.CourseExplain = obj.CourseExplain;
                cor.CourseName = obj.CourseName;
                cor.CourseTypeID = Convert.ToInt32(obj.CourseType);
                cor.CourseTypeName = obj.CourseTypeName;
                cor.CreateTime = Convert.ToDateTime(obj.CreateTime);
                cor.CourseTeamID = Convert.ToInt32(obj.CourseTeamID);
                cor.IsCharge = Convert.ToInt32(obj.IsCharge);
                cor.JoinCount = Convert.ToInt32(obj.JoinCount);
                cor.Price = Convert.ToDouble(obj.Price);
                cor.RelatedCourse = obj.RelatedCourse;
                cor.StartTime = Convert.ToDateTime(obj.StartTime);
                cor.TeacherID = Convert.ToInt32(obj.TeacherID);
                cor.TeacherID = Convert.ToInt32(obj.TeacherID);
                cor.TeacherName = obj.TeacherName;
                cor.TeacherTitle1 = obj.TeacherTitle1;
                cor.TeacherTitle2 = obj.TeacherTitle2;
                if (obj.TeacherImg != null && obj.TeacherImg != "")
                {
                    string imgPath = System.Configuration.ConfigurationManager.AppSettings["ImagePath"];
                    cor.TeacherImg = imgPath + obj.TeacherImg;
                }
                cor.TeacherInfo = obj.TeacherInfo;
                courseList.Add(cor);
            }
            return courseList;

        }

        /// <summary>
        /// 查询评价列表
        /// </summary>
        /// <param name="teamID"></param>
        /// <returns></returns>
        public static List<Proc_Comment_SelectListResult> GetCommentList(int teamID)
        {
            CourseDBDataContext db = new CourseDBDataContext();
            return db.Proc_Comment_SelectList(teamID).ToList();
        }

        /// <summary>
        /// 添加课程评价
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="courseID"></param>
        /// <param name="courseText"></param>
        /// <returns></returns>
        public static bool AddComment(string userID, int courseID, string courseText)
        {
            CourseDBDataContext db = new CourseDBDataContext();
            db.Proc_Comment_Add(userID, courseID, courseText);
            return true;
        }

        /// <summary>
        /// 点赞
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="commentID"></param>
        /// <returns>-1:已点过赞     0:成功</returns>
        public static int CommentParise(string userID, int commentID)
        {
            CourseDBDataContext db = new CourseDBDataContext();
            return db.Proc_Comment_Parise(userID, commentID);
        }

        /// <summary>
        /// 获取我的课程
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public static List<Proc_Comment_MyCommentResult> GetMyCommentList(string userID)
        {
            CourseDBDataContext db = new CourseDBDataContext();
            return db.Proc_Comment_MyComment(userID).ToList();
        }

        /// <summary>
        /// 领取赠送课程
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="courseID"></param>
        /// <param name="orderID"></param>
        /// <returns></returns>
        public static int GetCourse(string userID, int courseID, string orderID)
        {
            CourseDBDataContext db = new CourseDBDataContext();
            return db.Proc_UserCourse_Give(userID, courseID, orderID);
        }

        /// <summary>
        /// 获取开课提醒列表
        /// </summary>
        /// <returns></returns>
        public static List<Proc_CourseInfo_SelectStartListResult> GetStartCorseList()
        {
            CourseDBDataContext db = new CourseDBDataContext();
            return db.Proc_CourseInfo_SelectStartList().ToList();
        }
    }
}
